package com.pda.common.pages.main;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.pda.common.R;
import com.pda.common.bean.LoginBean;
import com.pda.common.pages.codereplace.CodeReplaceActivity;
import com.pda.common.pages.codereset.CodeResetActivity;
import com.pda.common.pages.coderevert.CodeRevertActivity;
import com.pda.common.pages.inbill.InBillActivity;
import com.pda.common.pages.outbill.OutBillActivity;
import com.pda.common.pages.productbill.ProductBillActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater;
    List<LoginBean.DataBean.ShowInfoBean> mlists = new ArrayList<LoginBean.DataBean.ShowInfoBean>();

    public MainAdapter(Context context, List<LoginBean.DataBean.ShowInfoBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_main, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Glide.with(mContext).load(mlists.get(position).getIcon()).into(viewHolder.tiv);
        viewHolder.ttv.setText(mlists.get(position).getText());
        viewHolder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                switch (mlists.get(position).getId()) {
                    case 1:
                        intent.setClass(mContext, InBillActivity.class);
                        mContext.startActivity(intent);
                        break;
                    case 2:
                        intent.setClass(mContext, OutBillActivity.class);
                        mContext.startActivity(intent);
                        break;
                    case 3:
                        intent.setClass(mContext, ProductBillActivity.class);
                        mContext.startActivity(intent);
                        break;
                    case 4:
                        intent.setClass(mContext, CodeReplaceActivity.class);
                        mContext.startActivity(intent);
                        break;
                    case 5:
                        intent.setClass(mContext, CodeResetActivity.class);
                        mContext.startActivity(intent);
                        break;
                    case 6:
                        intent.setClass(mContext, CodeRevertActivity.class);
                        mContext.startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tiv)
        ImageView tiv;
        @BindView(R.id.ttv)
        TextView ttv;
        @BindView(R.id.rl)
        RelativeLayout rl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

